<?php

	if(!class_exists(class_manage))
		{
			exit('Nhin gi ?');
		}
		
	class class_search extends class_manage
		{

			/*====================================/*
			*| Ham lay cac chuyen muc
			*| chuyen_muc()
			*| Input:  None; Output: html cua select box
			/*====================================*/
			function chuyen_muc()
				{
					$select_main = $this->data->query("SELECT id,tieu_de FROM category_index WHERE main_id=0 ORDER BY thu_tu");
					while( $main = $select_main->fetch_assoc() )
						{
							$return .= '<option value="'.$main['id'].'">'.$main['tieu_de'].'</option>';
							$select_sub = $this->data->query("SELECT id, tieu_de FROM category_index WHERE main_id='$main[id]' ORDER BY thu_tu");
								while($sub = $select_sub->fetch_assoc())
									{
										$return .= '<option value="'.$sub['id'].'">---'.$sub['tieu_de'].'</option>';
									}
						}
					return $return;
				}
			
			/*====================================/*
			*| Ham goi_y_ket_qua()
			*| Lay ra ket qua goi y cho san pham
			*| Ham tra ve 1 array lon
			*| Cac value trong array lon la 1 array mang ket qua
			/*====================================*/
			function goi_y_ket_qua($keywords, $table, $column)
				{
				$return = array();
					// Loc input keywords, tables
					$keywords = $this->data->escapeString($keywords);
					$table = $this->data->escapeString($table);
					$column = $this->data->escapeString($column);
					
					// Neu input ko co space
					if(!strpos($keywords, ' '))
					{
						$query = $this->data->query("SELECT * FROM ".$table." WHERE ".$column." LIKE '%".$keywords."%' LIMIT 30");
						while($result = @$query->fetch_assoc())
							{
								
								array_push($return, $result);
							}
					}
					// Neu input co space
					else
					{
							$num_key = substr_count($keywords, ' ');
						$keywords_array = explode(' ', $keywords);
						foreach($keywords_array as $key => $value)
							{
								$query_or_like = " OR ".$column." LIKE '%".$value."%' ";
							}
							
						$query = $this->data->query("SELECT * FROM ".$table." WHERE ".$column." LIKE '%".$keywords."%'".$query_or_like." LIMIT 30");
							while($result = @$query->fetch_assoc())
								{
									array_push($return, $result);
								}
					}
					
					return $return;
				}
				
			/*====================================/*
			*| Ham ket_qua_tim_kiem()
			*| Lay ra ket qua cua tim kiem
			*| Input la 1 array chua cac thong tin tim kiem
			*| Cac key cua input la:
			*| tieu_de, danhmuc, khoanggia ,gia_nho_nhat, gia_lon_nhat, noi_rao, datepost
			*|
			*| $page la phan trang can xem, $result la so ket qua / trang
			*=====================================
			*|
			*| Output la 1 array lon'
			*| Cac value trong array lon la array nho mang ket qua
			/*====================================*/			
			
			function ket_qua_tim_kiem($array, $page=1, $result=10)
				{	
					// Phan trang theo input
					$page = intval($page); if($page<1) {$page = 1;};
					$result = intval($result); if($result<10) {$result = 10;};
					$dautrang = ($page-1)*$result;
					
					// Build return variable
					$return = array();
					
					// Build khoang gia
					if($array['khoanggia']!='' or !empty($array['khoanggia']))
						{
						$khoanggia = explode('--',$array['khoanggia']);
						$array['gia_nho_nhat'] = intval($khoanggia[0]);
						$array['gia_lon_nhat'] = intval($khoanggia[1]);
						}
					
					// Build datepost
					if($array['datepost']!='' or !empty($array['datepost']))
						{
							$array['datepost'] = time() - intval($array['datepost'])*86400;
						}

					// Loc input
					foreach($array as $key => $value)
						{
							$array[$key] = $this->data->escapeString($value);
							if($array[$key]=='' or empty($array[$key]))
								{
								unset($array[$key]);
								}
						}
					

					
					// Build query tieu de
					if(isset($array['tieu_de']))
						{
							// Build like query foreach child keywords
							if(strpos($array['tieu_de'], ' '))
								{
									foreach( explode(' ', $array['tieu_de']) as $key => $value)
										{
											$query_tieu_de_like .= " OR tieu_de LIKE '%".$value."%' ";
										}
								}
							else
								{
									$query_tieu_de_like = '';
								}
								
						$query_tieu_de = "(tieu_de LIKE '%".$array['tieu_de']."%'".$query_tieu_de_like.")";
						}
					else
						{
						$query_tieu_de = "1=1";
						}
						
					// Build query danhmuc
					if(isset($array['danhmuc']))
						{
						$query_danhmuc = "(main_cat='".$array['danhmuc']."' OR sub1='".$array['danhmuc']."' OR sub2='".$array['danhmuc']."')";
						}
					else
						{
						$query_danhmuc = "1=1";
						}
						
					// Build query gia
					if(isset($array['gia_nho_nhat']) AND isset($array['gia_lon_nhat']))
						{
						$query_gia = "(gia>='".$array['gia_nho_nhat']."' AND gia <='".$array['gia_lon_nhat']."')";
						}
					elseif(isset($array['gia_nho_nhat']) AND !isset($array['gia_lon_nhat']))
						{
						$query_gia = "gia>=".$array['gia_nho_nhat'];
						}
					elseif(!isset($array['gia_nho_nhat']) AND isset($array['gia_lon_nhat']))
						{
						$query_gia = "gia<=".$array['gia_lon_nhat'];
						}
					else
						{
						$query_gia = "1=1";
						}
						
					// Build query khu vuc
					if(isset($array['noi_rao']))
						{
						$query_noi_rao = "noi_rao='".$array['noi_rao']."'";
						}
					else
						{
						$query_noi_rao = "1=1";
						}
						
					// Build query datepost
					if(isset($array['datepost']))
						{
						$query_datepost = "datepost>='".$array['datepost']."'";
						}
					else
						{
						$query_datepost = "1=1";
						}
						
					// Build main query
					$query = $this->data->query("SELECT * FROM sanpham WHERE ".$query_tieu_de." AND ".$query_danhmuc." AND ".$query_gia." AND ".$query_noi_rao." AND ".$query_datepost." LIMIT $dautrang,$result");
					
					// Build return
						while($result = @$query->fetch_assoc())
							{
								array_push($return, $result);
							}
					// So luong ket qua
				//	$return['so_ket_qua'] = $query->num_rows;
					return $return;
				}
			
		}

?>